package com.hxzf.mvp.web;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.fill.Column;
import com.baomidou.mybatisplus.generator.fill.Property;
import com.hxzf.mvp.commons.BaseController;
import com.hxzf.mvp.commons.BaseServiceImpl;
import com.hxzf.mvp.commons.IBaseService;
import com.hxzf.mvp.commons.db.CrudMapper;
import com.hxzf.mvp.commons.domain.BaseEntity;
import com.hxzf.mvp.commons.domain.Resp;

import java.util.Collections;

/**
 * 代码生成器
 *
 * @author Kai
 * @date 2021/12/17 12:45
 **/
public class CodeGen {

    private static final String DB_URL = "jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8&useSSL=false&&rewriteBatchedStatements=true";
    private static final String DB_USERNAME = "root";
    private static final String DB_PASSWD = "root";
    private static final String AUTHOR = "hxzf";
    private static final String OUTPUT_DIR = "D://gen";

    private static final DataSourceConfig.Builder DATA_SOURCE_CONFIG = new DataSourceConfig
            .Builder(DB_URL,
            DB_USERNAME,
            DB_PASSWD);

    public static void main(String... args) {
        FastAutoGenerator.create(DATA_SOURCE_CONFIG)
                // 全局配置
                .globalConfig((scanner, builder) -> builder.author(AUTHOR)
                        .enableSwagger() // 开启 swagger 模式
                        .fileOverride() // 覆盖已生成文件
                        .outputDir(OUTPUT_DIR) // 指定输出目录
                        .dateType(DateType.ONLY_DATE) // 时间策略
                )

                // 包配置
                .packageConfig((scanner, builder) -> builder.parent("com.hxzf")
                        .moduleName("mvp") // 设置父包模块名
                        .entity("dao.entity")
                        .service("service")
                        .serviceImpl("service.impl")
                        .mapper("dao")
                        .controller("web.controller")
                )

                // 策略配置
                .strategyConfig(builder -> {
                            builder.addInclude("t_test")
                                    .addTablePrefix("t_", "n_") // 设置过滤表前缀
                                    .entityBuilder()
                                    .superClass(BaseEntity.class)
                                    .disableSerialVersionUID()
                                    .enableLombok()
                                    .enableRemoveIsPrefix()
                                    .versionColumnName("version")
                                    .versionPropertyName("version")
                                    .logicDeleteColumnName("deleted")
                                    .logicDeletePropertyName("deleted")
                                    .addTableFills(new Column("create_time", FieldFill.INSERT))
                                    .addTableFills(new Property("updateTime", FieldFill.INSERT_UPDATE))

                                    .controllerBuilder()
                                    .superClass(BaseController.class)
                                    .enableHyphenStyle()
                                    .enableRestStyle()

                                    .serviceBuilder()
                                    .superServiceClass(IBaseService.class)
                                    .superServiceImplClass(BaseServiceImpl.class)

                                    .mapperBuilder()
                                    .superClass(CrudMapper.class)
                                    .enableBaseResultMap()
                                    .enableBaseColumnList()
                                    .formatXmlFileName("%sMapper")
                            ;
                        }
                )

                // 模板配置
                .templateConfig(builder -> {
                    builder.entity("templates/domain.java")
                            .controller("templates/controller.java");
                })

                // 注入配置
                .injectionConfig(builder -> {
                    builder.customMap(Collections.singletonMap("respClassPackage", Resp.class.getName()))
                    ;
                })

                // 执行代码生成
                .execute();
    }
}
